package cn.hftoday.core;

import java.util.*;
import junit.framework.TestCase;
import cn.hftoday.db.*;
import cn.hftoday.util.*;
import cn.hftoday.org.*;

public class MapperTest extends TestCase {
	
	protected void setUp() throws Exception {
		ApplicationConfig.getInstance().load();
		ConnectionPool.getInstance().open();
		ConnectionPool.getInstance().beginTransaction();
	}
	
	protected void tearDown() throws Exception {
		ConnectionPool.getInstance().rollback();
		ConnectionPool.getInstance().close();
	}

	public void testQueryForObject() {
		testInsert();
		Conversation c = (Conversation)Mapper.getInstance().queryForObject("SelectConversationById", "vbnkrpmpdhw3ptn33rz4jhfn");
		assertEquals(c.getCode(), 10001);
	}

	public void testQueryForList() {
		//test object list
		testInsert();
		List list = Mapper.getInstance().queryForList("SelectConversation");
		assertTrue(list.size() >= 1);
		
		//test map list
		testQueryForSubclass();
		List list2 = Mapper.getInstance().queryForList("SelectPartyById", "23456788900opq");
		assertTrue(list2.size() == 1);
	}
	
	public void testQueryForSubclass() {
		Person p = new Person();
		p.setId("23456788900opq");
		p.setName("huitailang");
		p.setHidden(false);
		p.setCreateTime(CalendarParser.toCalendar("2009-01-14 00:00:00"));
		p.setUpdateTime(CalendarParser.toCalendar("2009-01-15 00:00:00"));
		p.setCode("050001");
		p.setBirthday(CalendarParser.toCalendar("2006-10-25 00:00:00"));
		p.setEnterTime(CalendarParser.toCalendar("2007-07-15 00:00:00"));
		p.setIdCard("03040-50607(8)");
		p.setTitle("mr");
		p.setPrivatePhone("911''1");
		p.setWorkPhone("119\\-2");
		p.setAddress("050001");
		Mapper.getInstance().insert("InsertPerson", p);
		
		Party p2 = (Party)Mapper.getInstance().queryForObject("SelectPartyById", "23456788900opq");
		assertEquals(p2.getName(), "huitailang");
	}

	public void testInsert() {
		Conversation c = new Conversation();
		c.setCode(10001);
		c.setCreateTime(CalendarParser.toCalendar("2008-12-31 08:05:30"));
		c.setUpdateTime(CalendarParser.toCalendar("2008-12-31 09:10:50"));
		c.setId("vbnkrpmpdhw3ptn33rz4jhfn");
		c.setRemember(true);
		Mapper.getInstance().insert("InsertConversation", c);
		
		Conversation c2 = (Conversation)Mapper.getInstance().queryForObject("SelectConversationById", "vbnkrpmpdhw3ptn33rz4jhfn");
		assertEquals(c2.getCode(), 10001);
		assertEquals(c2.getCreateTime(), c.getCreateTime());
		assertEquals(c2.getUpdateTime(), c.getUpdateTime());
	}
  
	public void testUpdate() {
		testInsert();
		Conversation c = (Conversation)Mapper.getInstance().queryForObject("SelectConversation", "vbnkrpmpdhw3ptn33rz4jhfn");
		c.setCode(10002);
		Mapper.getInstance().update("UpdateConversation", c);
  
		Conversation c2 = (Conversation)Mapper.getInstance().queryForObject("SelectConversationById", "vbnkrpmpdhw3ptn33rz4jhfn");
		assertEquals(c2.getCode(), 10002);
	}
  
	public void testDelete() {
		testInsert();
		
		Conversation c = (Conversation)Mapper.getInstance().queryForObject("SelectConversation", "vbnkrpmpdhw3ptn33rz4jhfn");
		Mapper.getInstance().delete("DeleteConversation", c);
  
		Conversation c2 = (Conversation)Mapper.getInstance().queryForObject("SelectConversationById", "vbnkrpmpdhw3ptn33rz4jhfn");
		assertEquals(c2, null);
	}
}
